from django.http import JsonResponse
from videos.models import Video
from user_center.const import MetaOk


# Create your views here.
def get_videos(request):
    # 获取参数
    video_id = request.GET.get('video_id')
    search_query = request.GET.get('search_query')
    # 从数据库里查询
    query = Video.objects.all()
    if search_query is None:
        query = query.filter(mask=False)
    if video_id is not None:
        query = query.filter(id=video_id)
    videos = query
    # 组装vo
    resp = {
        "meta": MetaOk,
        "videos": [],
        "total": videos.count()
    }
    for video in videos:
        resp["videos"].append(video.to_vo())
    if video_id is not None:
        if len(videos) == 1:
            resp["videos"][0]['video_url'] = videos[0].video_url
    return JsonResponse(resp)
